<?php
class ml_model_dbUserAccount extends Lib_datamodel_db {
    const PASSWORD_SALT = 'mE!1aDr';
    public function __construct() {
        $db_config = ml_factory::load_standard_conf ( 'db' );
        parent::__construct ( 'user_account', $db_config ['user_account'] );
    }
    /**
     * 邮件是否已占用
     *
     * @param string $email            
     * @return bool
     */
    public function isEmailExists($email, &$isExists) {
        if (! $this->init_db ())
            return false;
        
        /**
         *
         * @todo 单引号
         */
        
        $where = '`email` = "' . $this->escape ( $email ) . '"';
        $rs = $this->fetch_count ( $where );
        if (! $rs)
            return false;
        
        $isExists = $this->_data > 0 ? true : false;
        return true;
    }
    /**
     * 昵称是否已占用
     *
     * @param string $nick            
     * @return bool
     */
    public function isNickExists($nick, &$isExists) {
        if (! $this->init_db ())
            return false;
        
        $where = '`nickname` = "' . $this->escape ( $nick ) . '"';
        $rs = $this->fetch_count ( $where );
        if (! $rs)
            return false;
        
        $isExists = $this->_data > 0 ? true : false;
        return true;
    }
    /**
     * 创建ACCOUNT记录
     *
     * @param array $arrData
     *            nickname
     *            password
     *            email
     * @return bool
     */
    public function createAccount($arrData) {
        if (! $this->init_db ())
            return false;
        
        $arrInsert = array (
                'nickname' => $arrData ['nickname'],
                'password' => $this->_password_encrypt ( $arrData ['password'] ),
                'email' => $arrData ['email'],
                'status' => ML_USERSTATUS_OK,
                'reg_ip' => Tool_ip::get_real_ip () 
        );
        
        return $this->insert ( $arrInsert );
    }
    /**
     * 第三方注册创建ACCOUNT记录
     *
     * @param string $nickname            
     * @return bool
     */
    public function createAccountBy3rd($nickname) {
        if (! $this->init_db ())
            return false;
        
        $arrInsert = array (
                'nickname' => $nickname,
                'email' => $nickname,
                'status' => ML_USERSTATUS_THIRD,
                'verify_email' => ML_NOEMAIL,
                'reg_ip' => Tool_ip::get_real_ip () 
        );
        
        return $this->insert ( $arrInsert );
    }
    /**
     * 对密码进行加密
     *
     * @param string $password            
     * @return string
     */
    private function _password_encrypt($password) {
        $pw_encode = self::PASSWORD_SALT . $password . substr ( md5 ( $password ), 7, 2 ) . strlen ( $password );
        return md5 ( $pw_encode );
    }
    
    /**
     * 获取账号信息
     */
    public function getAccountById($uid) {
        if (empty($uid)) {
            return false;
        } else {
            if (! $this->init_db ())
                return false;
            $sql = 'SELECT * FROM ' . $this->table . ' WHERE uid = ' . $uid;
            $rs = $this->fetch_row ( $sql );
            $data = $this->get_data ();
            return $rs ? $data : false;
        }
    }
    public function getAccountByNick($nickname) {
        if (! $this->init_db ())
            return false;
        
        $sql = 'SELECT * FROM ' . $this->table . ' WHERE nickname = \'' . $this->escape ( $nickname ) . '\'';
        $rs = $this->fetch_row ( $sql );
        return $rs ? $this->_data : false;
    }
    public function getAccountByEmail($email) {
        if (! $this->init_db ())
            return false;
        
        $sql = 'SELECT * FROM ' . $this->table . ' WHERE email = \'' . $this->escape ( $email ) . '\'';
        $rs = $this->fetch_row ( $sql );
        return $rs ? $this->_data : false;
    }
    public function checkPassword($pwd, $account, $nickOrEmail = 0) {
        if (! $this->init_db ())
            return false;
        
        $account = $this->escape ( $account );
        $pwd_encrypt = $this->_password_encrypt ( $pwd );
        
        if ($nickOrEmail) {
            $sql = 'SELECT * FROM ' . $this->table . ' WHERE email = \'' . $account . '\' AND password = \'' . $pwd_encrypt . '\'';
        } else {
            $sql = 'SELECT * FROM ' . $this->table . ' WHERE nickname = \'' . $account . '\' AND password = \'' . $pwd_encrypt . '\'';
        }
        return $this->fetch_row ( $sql );
    }
    public function getUidNickByPage($page, $pagesize) {
        if (! $this->init_db ())
            return false;
        
        $start = ($page - 1) * $pagesize;
        $sql = 'select uid,nickname from ' . $this->table . ' limit ' . $start . ' , ' . $pagesize;
        return $this->fetch ( $sql );
    }
    
    /**
     * 更改密码
     * zhanglei11
     * $uid 用户uid
     * $pwd 已经加密过的密码
     */
    public function updatePwd($uid, $pwd, $email = '') {
        if (! $this->init_db ())
            return false;
        
        $pwd = $this->_password_encrypt ( $pwd );
        // 第三方绑定才会有email,改变verify_email
        if (strlen ( $email ) > 0)
            $sql = 'UPDATE ' . $this->table . ' SET password = ' . "'" . $pwd . "'" . ',email = \'' . $email . '\',verify_email=' . ML_NOTVERIFY . ' WHERE uid = ' . $uid;
        else
            $sql = 'UPDATE ' . $this->table . ' SET password = \'' . $pwd . '\' WHERE uid = ' . $uid;
        $rs = $this->query ( $sql );
        return $rs ? $rs : false;
    }
    
    /**
     * 比较密码
     * zhanglei11
     * 
     * @param 用户表里的密码 $pwd            
     * @param 用户输入的密码 $input_pwd            
     */
    public function comparePwd($pwd, $input_pwd) {
        $encrypt = $this->_password_encrypt ( $input_pwd );
        return $pwd == $encrypt ? true : false;
    }
    
    /**
     * 修改昵称
     * wangtao5@
     * 
     * @param 用户表里uid $uid            
     * @param 用户昵称 $nickname            
     */
    public function updateNick($uid, $nickname) {
        if (! $this->init_db ())
            return false;
        $where = '`uid` = ' . $uid;
        $data ['nickname'] = $nickname;
        // 投队列
        
        $arr = array (
                'uid' => $uid,
                'nick' => $nickname 
        );
        ml_tool_sendMq::update_nick ( $arr );
        ml_tool_sendMq::search_user ( 'A', $uid );
        $rs = $this->update ( $data, $where );
        return $rs;
    }
    
    /**
     * 批量换取昵称
     *
     * 返回数据格式
     * array(
     * [uid] => [nick],
     * 10005 => '小顶',
     * )
     * 
     * @param array $aUid            
     * @return bool
     */
    public function getNicksByUids($aUid) {
        if (! $this->init_db ())
            return false;
        
        $data = array ();
        
        $sUid = implode ( ',', array_unique ( $aUid ) );
        
        $sql = "select `nickname`,`uid` from `$this->table` where `uid` in (" . $sUid . ")";
        
        $rs = $this->fetch ( $sql );
        
        if (! $rs)
            return false;
        
        $data = Tool_array::format_2d_array ( $this->get_data (), 'uid', Tool_array::FORMAT_VALUE2VALUE2, 'nickname' );
        $this->set_data ( $data );
        
        return true;
    }
    
    /**
     * 更改用户的邮箱标示
     * 
     * @param unknown_type $verify            
     * @param unknown_type $email
     *            @zhanglei11
     */
    public function updateVerifyEmail($verify, $uid) {
        $row ['verify_email'] = $verify;
        $row ['status'] = ML_USERSTATUS_OK;
        $where = '`uid` = ' . $uid;
        return $this->update ( $row, $where );
    }
}
?>